package ajax;

import java.io.IOException;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.DealModel;

import search.DealSearchData;

import com.google.gson.Gson;

@SuppressWarnings("serial")
public class DealAjaxServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		String keywords = (String) request.getParameter("keywords");
		TreeMap<String, String> result = new TreeMap<String, String>();
		if (keywords != null) {
			DealSearchData dealSearch = new DealSearchData();
			dealSearch.setPage(1);
			dealSearch.setLimit(100);
			dealSearch.searchAdvance(keywords, null, null, 99);

			if (dealSearch.getListResult().size() > 0) {
				for (DealModel deal : dealSearch.getListResult()) {
					result.put(deal.getName(), deal.getDealId());
				}
			}
		}

		String json = new Gson().toJson(result);
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
		return;
	}
}
